﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.SharePoint;
using System.Diagnostics;

namespace SharePointOfView.Diagnostics
{
    /// <summary>
    /// Write in the 'Windows SharePoint Services 3' source EventLog  
    /// </summary>
    public static class SPEventLog
    {
        private const string STR_SOURCE = "Windows SharePoint Services 3";

        /// <summary>
        /// Writes the specified message.
        /// </summary>
        /// <param name="message">The message.</param>
        public static void Write(string message)
        {
            if (EventLog.SourceExists(STR_SOURCE))
            {
                EventLog.WriteEntry(STR_SOURCE, message, EventLogEntryType.Error);
            }
        }

        /// <summary>
        /// Writes the specified message.
        /// </summary>
        /// <param name="message">The message.</param>
        /// <param name="eventLogEntryType">Type of the event log entry.</param>
        public static void Write(string message, EventLogEntryType eventLogEntryType)
        {
            if (EventLog.SourceExists(STR_SOURCE))
            {
                EventLog.WriteEntry(STR_SOURCE, message, eventLogEntryType);
            }

        }


        /// <summary>
        /// Writes the specified ex.
        /// </summary>
        /// <param name="ex">The ex.</param>
        public static void Write(Exception ex)
        {
            if (EventLog.SourceExists(STR_SOURCE))
            {
                StringBuilder sb = new StringBuilder();
                sb.AppendLine("Message: " + ex.Message);
                sb.AppendLine("Inner Exception: " + ex.InnerException);
                sb.AppendLine("Call Stack: " + ex.StackTrace);

                EventLog.WriteEntry(STR_SOURCE, sb.ToString(), EventLogEntryType.Error);
            }
        }

    }
}
